const { mongoose, db } = require("../utils/mongoose");
let schema = mongoose.Schema({
  id: { type: String, required: true },
  date: { type: Date, required: true },
  todo: { type: String, required: true },
  type: { type: String, required: false },
  status: { type: String, required: false },
  value: { type: Number, required: false },
  coin: { type: Number, required: false },
});

let Collection = mongoose.model("todos", schema);

//新增
const addToDo = (data) => {
  let model = new Collection(data);
  return model
    .save()
    .then((res) => res)
    .catch((err) => {
      console.log(err);
      return false;
    });
};
//查询
// .sort({'date':-1})//按插入时间倒序排列
// date:{$gte: sDate, $lte: eDate}
const findToDoList = ({ id, sDate, eDate }) => {
  return Collection.find({ id, date: { $gte: sDate, $lte: eDate } })
    .sort({ date: -1 })
    .then((res) => res)
    .catch((err) => {
      console.log(err);
      return false;
    });
};
//操作
const editToDoList = (query, updated) => {
  return Collection.updateOne(query, updated)
    .then((res) => res)
    .catch((err) => {
      console.log(err);
      return false;
    });
};

const update = (query, updated) => {
  return Collection.updateOne(query, updated)
    .then((res) => res)
    .catch((err) => {
      console.log(err);
      return false;
    });
};
module.exports = {
  addToDo,
  findToDoList,
  editToDoList,
  update
};
